window.onload = function () {
    //定义用户类
    class  User{
        constructor(username,password) {
            this.username = username;
            this.password = password;
        }
    }
    let userInfo =[];//定义数组保存用户的用户名和密码信息
    let oLoginPanel = document.querySelector(".login-panel");//登录面板
    let oRegisterPanel = document.querySelector(".register-panel") ;//注册面板
    let oSubmitBtn = document.querySelector(".submit-btn");//登录按钮
    let oRegister = document.querySelector(".toRegister");//去注册链接
    let oRegisterBack = document.querySelector(".register-footer>a");//注册页面的返回登录链接
    let oForgetPassword = document.querySelector(".forgetPassword");//登录面板的忘记密码链接
    let oUpdatePanel = document.querySelector(".updatePassword-panel");//改密面板
     let oUpdateBtn = document.querySelector(".update-btn");//改密页面的确定按钮
    let oUpdateBack = document.querySelector(".update-footer");//改密页面的返回登录链接
    let oRegisterSuccessBtn = document.querySelector(".register-btn");//注册按钮
    //登录按钮的点击事件
    oSubmitBtn.onclick = function (){
        let login_username = document.querySelector("#login_username").value;
        let login_password = document.getElementById("login_password").value;
      if (isInList2(login_username, login_password)) {
          window.location.href = "http://localhost:63342/h5jsCode/src/javascriptCode/%E5%AD%A6%E7%94%9F%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/student.html"
      }else{
          alert("用户名不存在");
      }
      return false;
    }
    //判断是否在数组中的方法
    function isInList2(name,password){

        for (let i = 0;i < userInfo.length;i++){
            if (userInfo[i].username === name && userInfo[i].password === password){
                return true;
            }
        }
        return false;
    }
    //去注册页面的点击事件
    oRegister.onclick = function () {
        oLoginPanel.style.display = 'none';
        oRegisterPanel.style.display = "block";
    }
    // 注册页面的返回登录链接的点击事件
    oRegisterBack.onclick = function () {
        oRegisterPanel.style.display = "none";
        oLoginPanel.style.display = "block";
    }
    //为注册用户名和密码绑定离焦事件
    document.getElementById("register_username").onblur = checkUsername;
    document.getElementById("register_password1").onblur = checkPassword;
    document.getElementById("register_password2").onblur = checkPassword2;
    //注册页面检查用户注册的方法
    function checkUsername(){
        let username = document.getElementById("register_username").value;
        //正则判断
        let reg_username = /^\w{6,12}$/;
        let flag = reg_username.test(username);
        let s_username = document.getElementById("s_username");
        if (flag){
            //若正确格式显示对勾
            s_username.innerHTML =  "<img style='width: 35px;height: 25px' src='images/gou.png'/>";
        }
        else{
            //不正确就报错
            s_username.innerHTML = "用户名格式有误";
        }
        return flag;
    }
    function checkPassword() {
        let password1 = document.getElementById("register_password1").value;
        let reg_password = /^\w{6,12}$/;

        let flag = reg_password.test(password1);
        let s_password1 = document.getElementById("s_password1");
        if (flag){
            //提示绿色对
            s_password1.innerHTML =  "<img width='35' height='25' src='images/gou.png'/>";
        }
        else{
            s_password1.innerHTML = "密码格式有误";
        }
        return flag;
    }
    function checkPassword2(){
       let  password1 = document.getElementById("register_password1").value;
        let password2 = document.getElementById("register_password2").value;
        let s_password2 = document.getElementById("s_password2");
        if (password1 === password2){
            //提示绿色对
            s_password2.innerHTML =  "<img width='35' height='25' src='images/gou.png'/>";
        }
        else{
            s_password2.innerHTML = "输入不一样";
        }
        return password1===password2;

    }
    oRegisterSuccessBtn.onclick = function (){
        let s_btn = document.getElementById("s_btn");
          if( checkUsername() && checkPassword() && checkPassword2()){
              //将用户放入数组
              let password = document.getElementById("register_password1").value;
              let username = document.getElementById("register_username").value;
              let user = new User(username,password);
              userInfo.push(user);
              //提示注册成功
              s_btn.innerHTML = "注册成功";
          }
          else{
              s_btn.innerHTML = "注册失败";
          }
        return false;
    }
    // 登录面板的忘记密码链接的点击事件
    oForgetPassword.onclick = function () {
        oLoginPanel.style.display = 'none';
        oUpdatePanel.style.display = "block";
    }

    //改密离焦事件
    document.getElementById("update_username").onblur = checkUpdateUsername;
    document.getElementById("update_password2").onblur = checkPassword3;
    function checkUpdateUsername() {
        let username = document.getElementById("update_username").value;
        if (!isInLIst(username)){
            let s_updateName = document.querySelector("#s_updateName");
            s_updateName.innerHTML = "用户名不存在";
            return false;
        }else {
            return true;
        }

    }
    //判断是否在数组里面方法
    function isInLIst(username) {

        for (let i = 0;i < userInfo.length;i++){
            if (userInfo[i].username == username){
                return true;
            }
        }
        return false;
    }
    //检验重置的密码
    function checkPassword3(){
        let updatePassword = document.getElementById("update_password1").value;
        let updatePassword2 = document.getElementById("update_password2").value;
        let s_UpdatePassword = document.getElementById('s_UpdatePassword');
        if (updatePassword === updatePassword2){
            s_UpdatePassword.innerHTML = '正确';
            return true;
        }else{
            s_UpdatePassword.innerHTML = '输入错误';
            return false;
        }
    }
//改密页面确定按钮的点击事件
    oUpdateBtn.onclick = function(){
        let s_update = document.getElementById("s_update");
        if (checkPassword3()&&checkUpdateUsername()){
            let username = document.getElementById("update_username").value;
            for (let i = 0; i < userInfo.length; i++) {
                if (username==userInfo[i].username){
                    userInfo[i].password =  document.getElementById("register_password2").value;

                    s_update.innerHTML = "改密成功";
                }
            }
        }
        else {
            s_update.innerHTML = "改密失败";
        }
        return false;
    }
    // 改密页面返回登录事件
    oUpdateBack.onclick = function () {
        oUpdatePanel.style.display = 'none';
        oLoginPanel.style.display = "block";
    }

}
